package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.report;

import android.util.SparseBooleanArray;
import android.widget.CheckedTextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicReportTypeResult;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class ReportTypeAdapter extends BaseQuickAdapter<DynamicReportTypeResult, BaseViewHolder> {

    private SparseBooleanArray sparseBooleanArray;
    private int lastCheckIndex = -1;


    public ReportTypeAdapter() {
        super(R.layout.recycle_item_report_card_type_list, new ArrayList<>());
        sparseBooleanArray = new SparseBooleanArray(getItemCount());
    }

    public void check(int position) {
        if (position == lastCheckIndex) {
            return;
        }
        if (lastCheckIndex >= 0) {
            sparseBooleanArray.put(lastCheckIndex, false);
            notifyItemChanged(lastCheckIndex);
        }
        lastCheckIndex = position;
        sparseBooleanArray.put(lastCheckIndex, true);
        notifyItemChanged(lastCheckIndex);
    }

    public DynamicReportTypeResult getCheckItem() {
        return getItem(lastCheckIndex);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, DynamicReportTypeResult item) {
        CheckedTextView cb = holder.getView(R.id.cb_report_type);
        cb.setText(item.getLabel());

        cb.setChecked(sparseBooleanArray.get(holder.getAdapterPosition()));

    }
}
